package com.qidian.buy.product.controller;

import com.qidian.buy.entity.Item;
import com.qidian.buy.entity.ItemDesc;
import com.qidian.buy.product.service.IItemService;
import com.qidian.buy.util.pojo.BuyResult;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author 戴着假发的程序员
 * @since 2023-05-18
 */
@RestController
@RequestMapping("/buy/item")
@CrossOrigin
public class ItemController {

    @Autowired
    private IItemService itemService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @PostMapping("/save")
    public BuyResult save(Item item, ItemDesc itemDesc){
        BuyResult buyResult = itemService.saveItem(item, itemDesc);
        if(buyResult.getCode()==200){
            // 发送消息，消息中只要包含我们的商品的id集合
            rabbitTemplate.convertAndSend("item-add-exchange",null,item.getId());
        }
        return buyResult;
    };

    @GetMapping("/list")
    public BuyResult<Item> list(@RequestParam(defaultValue = "1")int page,@RequestParam(defaultValue = "10")int limit,String title){
        return itemService.queryByPage(page,limit,title);
    }

    @GetMapping("/get")
    public Item get(Long itemId){
        return itemService.getById(itemId);
    }

    @GetMapping("/detail")
    public BuyResult detail(Long id){
        return itemService.queryById(id);
    }
}
